//
//  MineCollectionViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/22.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "MineCollectionViewController.h"
#import "BaseHeader.h"
#import "CollectTableViewCell.h"
#import "PreferentialModel.h"
#import "PreViewController.h"
#import "DataBase.h"
@interface MineCollectionViewController ()<UITableViewDataSource , UITableViewDelegate>

@property (nonatomic , strong) NSMutableArray * array ;
@property(nonatomic,strong)UITableView *collectTableView;



@end

@implementation MineCollectionViewController

-(void)viewWillAppear:(BOOL)animated
{
    
    _array = [[NSMutableArray alloc] init];
    _array = [[DataBase shareDataBase]selectAll];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIImageView * shoucang = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * suitableScreen_Width)];
    shoucang.image = [UIImage imageNamed:@"shoucang.jpg"];
    [self.view addSubview:shoucang];
    shoucang.alpha = 0.85 ;
    
    
    UILabel * shoucangb = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 * suitableScreen_Width, mainScreen_Width, 50 * suitableScreen_Width)];
    shoucangb.alpha = 0.8 ;
    
    [shoucangb setText:@"我的收藏"];
    [shoucangb setTextAlignment:NSTextAlignmentCenter];
    [shoucangb setFont:[UIFont systemFontOfSize:30]];
    [shoucangb setBackgroundColor:[UIColor clearColor]];
    [shoucangb setTextColor:[UIColor whiteColor]];
    [shoucang addSubview:shoucangb];
    
    UIView * viewHe = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 60 * suitableScreen_Width)];
    [shoucang addSubview:viewHe];
    [shoucang setBackgroundColor:[UIColor lightGrayColor]];
    
    // [[DataBase shareDataBase]openDB];
    
    
    
    
    if (_collectTableView == nil) {
        
        _collectTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200 * suitableScreen_Width, mainScreen_Width , mainScree_Height - 200 * suitableScreen_Width) style:UITableViewStylePlain];
        [self.view addSubview:_collectTableView];
        _collectTableView.delegate = self ;
        _collectTableView.dataSource = self ;
        
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125 * suitableScreen_Width;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count ;
}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId = @"cell";
    
    CollectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CollectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    PreferentialModel * model = [_array objectAtIndex:indexPath.row];
    [cell getCollect:model];
    [cell.phoneLabel addTarget:self action:@selector(phone : ) forControlEvents:UIControlEventTouchUpInside];
    cell.phoneLabel.tag = indexPath.row  ;
    
    return cell ;
}

//可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES ;
    
}

//编辑状态

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete ;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00000000000000000000001 ;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //        _array = [[DataBase shareDataBase] selectAll];
        PreferentialModel * model = [_array objectAtIndex:indexPath.row];
        
        [[DataBase shareDataBase] deleteStudent :model.modelName];
        [_array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        
    }
    
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PreViewController * re = [[PreViewController alloc]init];
    
    re.model = [_array objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:re animated:YES];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//打电话
-(void)phone : (UIButton * )sender
{
    PreferentialModel * model = [_array objectAtIndex:sender.tag ];
    
    NSString * p = [NSString stringWithFormat:@"tel://%@",model.phone];
    NSURL * url = [NSURL URLWithString:p];
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"号码不可用\n或您的备设不支持拨号功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
